<div class="widget">
	<div class="widget-header bg-blue">
		<i class="widget-icon icon-notebook"></i> 
		<span class="widget-caption"> 学员分班 </span>
		<div class="widget-buttons">
			<a href="javascript:void(0);" onclick="handlerCancel(this);" title="关闭"
				 data-toggle="dispose" data-refresh-table="#table-contract"> <i class="fa fa-times"></i> </a> 
		</div>
	</div>
	<div class="widget-body">
		<!-- TODO 要分班的学员 -->
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 要分班的学员 </span>
			</div>
			<div class="widget-body editor-body-sm">
				<table data-toggle="table"
							 data-id-field="id" 
							 data-unique-id="id" 
							 data-check-on-init="true" 
							 data-mobile-responsive="true" id="table-student-todo">
					<thead>
						<tr>
							<th data-checkbox="true"></th>
							<th data-field="id" 				data-visible="false" data-switchable="false">ID</th>
							<th data-field="student" 		data-align="center" >会员</th>
							<th data-field="nickname" 	data-align="center" >昵称</th>
							<th data-field="moonage" 		data-align="center" >月龄</th>
							<th data-field="relation" 	data-align="center" >家长</th>
							<th data-field="mobile" 		data-align="center" >手机</th>
							<th data-field="classes" 		data-align="center" data-width="30" data-formatter="renderClasses">所在班级</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
		
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-header">
				<i class="widget-icon icon-notebook"></i> 
				<span class="widget-caption"> 要分配的班级 </span>
			</div>
			<div class="widget-body editor-body-sm">
				<div id="querybar">
     			<div class="btn-group" id="btn-group-weeks">
				    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
   						<i class="glyphicon glyphicon-calendar"></i> 班级筛选 <span class="caret"></span>
					  </button>
					  <ul class="dropdown-menu">
					    <li><a href="javascript:void(0);" onclick="handlerWeek(this);">周一</a></li>
					    <li><a href="javascript:void(0);" onclick="handlerWeek(this);">周二</a></li>
					    <li><a href="javascript:void(0);" onclick="handlerWeek(this);">周三</a></li>
					    <li><a href="javascript:void(0);" onclick="handlerWeek(this);">周四</a></li>
					    <li><a href="javascript:void(0);" onclick="handlerWeek(this);">周五</a></li>
					    <li><a href="javascript:void(0);" onclick="handlerWeek(this);">周六</a></li>
					    <li><a href="javascript:void(0);" onclick="handlerWeek(this);">周日</a></li>
					  </ul>
				  </div>
				</div>
				<table data-ajax="handlerSearch" 
							 data-method="post" 
							 data-search="true" 
							 data-toolbar="#querybar" 
							 data-id-field="id" 
							 data-unique-id="id" 
							 data-pagination="true"  
							 data-check-on-init="true" 
							 data-click-to-select="true" 
							 data-side-pagination="server" 
							 data-mobile-responsive="true" 
							 data-form-search="#search-form-classes-todo" id="table-classes-todo">
					<thead>
						<tr>
							<th data-radio="true"></th>
							<th data-field="id" data-visible="false"></th>
							<th data-field="name" 			data-align="center">班级名称</th>
							<th data-field="teacher" 		data-align="center">负责老师</th>
							<th data-field="room" 			data-align="center">所在教室</th>
							<th data-field="students" 	data-align="center">学员数</th>
							<th data-field="toplimit" 	data-align="center">学员上限</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
		
		<div class="widget radius-bordered mgbt-xs-10">
			<div class="widget-body editor-body-sm">
				<form class="form-horizontal form-editor form-editor-customer">
					<div class="row">
						<div class="col-sm-6">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="createDate">处理时间</label>
								<div class="col-sm-8">
									<div class="input-group date date-selector">
										<input name="createDate" id="createDate" type="text" class="form-control"> 
										<span class="input-group-addon">
											<span class="fa fa-calendar"></span>
										</span>
									</div>
								</div>
							</div>
						</div>
						<div class="col-sm-5">
							<div class="form-group">
								<label class="col-sm-4 control-label" for="username">课程顾问</label>
								<div class="col-sm-8">
									<input type="text" name="username" id="username" class="form-control" readonly="readonly" /> 
									<input type="hidden" name="userid" id="userid" data-default-user/>
								</div>
							</div>
						</div>
					</div>
					
					<div class="widget-footer editor-footer-sm">
						<button type="button" class="btn btn-danger btn-sm" data-toggle="tooltip" data-placement="top" title="取消" onclick="handlerCancel(this);" data-refresh-table="#table-student">
							<i class="glyphicon glyphicon-remove"></i>
						</button>
						<button type="button" class="btn btn-primary btn-sm" data-toggle="tooltip" data-placement="top" title="保存" onclick="handlerDivide();">
							<i class="glyphicon glyphicon-ok"></i>
						</button>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>

<!-- 提交表单 ========================================================= -->
<form id="editor-form-divide" action="/miwawa/{dbname}/common/save" method="post" 
			name="com.app.miwawa.api.eam.entity.ClassesStudent">
</form>
<form id="editor-form-leaveLimit" action="/miwawa/{dbname}/common/save" method="post" 
			name="com.app.miwawa.api.eam.entity.StudentLeaveLimit">
</form>
<!-- 查询 表单 ========================================================= -->
<form id="search-form-classes-todo" action="/miwawa/{dbname}/common/search.json" method="post" 
			name="com.app.miwawa.api.eam.entity.view.ClassesView">
			<input type="hidden" name="type" value="早教" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="status" value="0" data-search-group="and" data-search-operation="EQ">
			<input type="hidden" name="invalid" value="F" data-search-group="and" data-search-operation="EQ">
</form>
		
<script type="text/javascript">
<!--	
	require(['domReady'],function(domReady){
		domReady(function () {
			loadStudents();
		});
	});
	
	// 加载学员数据
	function loadStudents(){
		jQuery("#userid",".form-editor-customer").val(userid);
		jQuery("#username",".form-editor-customer").val(username);
		
		var contracts = jQuery("#table-contract").data("selectedRow");
		jQuery("#table-student-todo").bootstrapTable().bootstrapTable("load", contracts);
		
		var type = jQuery(".nav.nav-tabs .active > a").data("type");
		jQuery(":hidden[name='type']", "#search-form-classes-todo").val(type);
		jQuery("#table-classes-todo").bootstrapTable().bootstrapTable("refresh");
		
		switch(type){
			case "早教": jQuery("#querybar").show(); break;
			case "托班": jQuery("#querybar").hide(); break;
		}
		require(['jquery.plugin.form'],function(form){
			form.datepicker(".date-selector");
			form.tooltip("[data-toggle='tooltip']");
		});
	}
	
	// 按周几筛选班级
	function handlerWeek(trigger){
		jQuery("#table-classes-todo").bootstrapTable('refreshOptions',{"searchText":trigger.innerText});
	}
	
	var studentname = [];
	var classesname;
	
	// 分班
	function handlerDivide(){
		var contracts = jQuery("#table-student-todo").bootstrapTable("getSelections");
		var classes = jQuery("#table-classes-todo").bootstrapTable("getSelections");
		if(contracts.length <= 0){
			jQuery.message.warning("请选择要分班的学员！");
		}else if(classes.length <= 0){
			jQuery.message.warning("请选择要安排的班级！");
		}else{
			var $form = jQuery("#editor-form-divide");
			var leavefrom = jQuery("#editor-form-leaveLimit");
			classesname = classes[0].name;
			jQuery.ajaxsend({
				url:"/miwawa/{dbname}/student/divide".replace("{dbname}",dbname),
				data:createIdsParam(contracts, classes),
				success:function(data){
					if(data && data.success){
						
						//初始化请假上限
						leavefrom.ajaxSubmit({
							url: leavefrom.attr("action").replace("{dbname}", dbname),
							data:createFormData(leavefrom, contracts, classes, "leave"),
							dataType: "json",
							resetForm: false
						});
						
						$form.ajaxSubmit({
							url: $form.attr("action").replace("{dbname}", dbname),
							data:createFormData($form, contracts, classes, "divide"),
							dataType: "json",
							resetForm: false,
							success: function(data){
								if(data) handlerCancel(jQuery('[data-toggle="dispose"]')[0]);
								if(studentname.length >0 ){
									jQuery.message.alert("学员:"+studentname.join("、") +"无法分到"+classesname+"。原因：在该班级上课时间内，这些学员已在其他班上课。");
								}
							}
						});

					}
				}
			});
		}
	}
	
	// 创建选中学员及班级的ID参数
	function createIdsParam(contracts, classes){
		var ids = {"studentids":[],"classesids":[]};
		for(var i = 0, size = contracts.length; i < size; i++){
			ids["studentids"].push(contracts[i].studentid);
		}
		ids["classesids"].push(classes[0].id);
		return {"studentids":ids["studentids"].join(","),"classesids":ids["classesids"].join(",")};
	}
	
	// 构建表单数据
	function createFormData(form, contracts, classes, tab){
		var userid = jQuery("[name='userid']").val();
		var username = jQuery("[name='username']").val();
		var createDate = jQuery("[name='createDate']").val();
		var data = {"entityClass":form.attr("name"), "beans":[]};
		for(var i = 0, size = contracts.length; i < size; i++){
			if(tab == "divide"){
				if(decide(contracts[i], classes[0])){
					studentname.push(contracts[i].student);
					continue;
				};
				 data.beans.push({
						"userid":userid,
						"username":username,
						"createDate":createDate,
						"student":contracts[i].student,
						"studentId":contracts[i].studentid,
						"classes":classes[0].name,
						"classesId":classes[0].id,
						"status":"0",
						"invalid":"F"
				});
			}else{
				if(decide(contracts[i], classes[0])){
					continue;
				};
				data.beans.push({
						"createDate":createDate,
						"studentId":contracts[i].studentid,
						"classesId":classes[0].id,
						"contractId":contracts[i].id,
						"topLimit":"-1",
						"status":"0",
						"invalid":"F"
				});
			}
		}
		return data;
	}
	
	//查询要分班的班级上课时间内,学员上课时间是否冲突,冲突返回 true,否则返回false 
	function decide(contracts, classes){
		var status = false;
		jQuery.ajax({
			url:"/miwawa/{dbname}/common/search.json".replace("{dbname}",dbname),
			type:"POST",
		  dataType:"json",
		  async:false,
			data:{
	       		"entityClass" : "com.app.miwawa.api.eam.entity.view.StudentClasses",
	          "and[0][operator]":"EQ",
	          "and[0][fieldName]":"studentid",					
	          "and[0][fieldValue]":contracts.studentid,
	          "and[1][operator]":"EQ",
	          "and[1][fieldName]":"type",					
	          "and[1][fieldValue]":"早教"
	  	},
	  	success:function(data){
	  		var list = data.rows;
	  		for(var n = 0; n<list.length; n++){
	  			var time = $moment("2016-09-14 "+list[n].classes.substring(2,7)+":00");
	  			if(classes.id != list[n].classesid && list[n].classes.substring(0,2) == classes.name.substring(0,2)){
	  				if($moment("2016-09-14 "+classes.name.substring(2,7)+":00").add(45,"m").isAfter(time) && time.isAfter($moment("2016-09-14 "+classes.name.substring(2,7)+":00").add(-45,"m"))){
	  					status = true;
	  					break;
	  				}
	  			}
	  		} 			  		
	  	}
		});	
		return status;
	}
	
//-->
</script>
